package dao_redis

import (
	"common/global"
	"context"
	"errors"
	"fmt"
)

const CATS = "car:%d:%d"
const CatUsers = "car:%d:*"

func CarProductDel(uid int64, pid int64) error {
	key := fmt.Sprintf(CATS, uid, pid)
	val, err := global.Client.Keys(context.Background(), key).Result()
	if err != nil {
		return errors.New("购物车查询失败")
	}
	for _, s := range val {
		err = global.Client.Del(context.Background(), s).Err()
		if err != nil {
			return errors.New("购物车商品移除失败")
		}
	}
	return nil
}
func CarDel(uid int64) error {
	key := fmt.Sprintf(CatUsers, uid)
	val := global.Client.Keys(context.Background(), key).Val()
	for _, s := range val {
		err := global.Client.Del(context.Background(), s).Err()
		if err != nil {
			return errors.New("购物车清空失败")
		}
	}
	return nil
}

// 购物车查询
func GetCarList(uid int64) ([]string, error) {
	key := fmt.Sprintf(CatUsers, uid)

	catVal, err := global.Client.Keys(context.Background(), key).Result()
	if err != nil {
		return nil, errors.New("当前用户购物车内没有商品")
	}

	return catVal, nil
}
